using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.BizTalk.BaseFunctoids;
using System.Reflection;
using System.Configuration;

namespace eliasen.eu.BizTalk.Functoids
{
    class ReadAppConfig : BaseFunctoid
    {
        public ReadAppConfig()
            : base()
        {
            //ID for this functoid
            this.ID = 6429;

            // resource assembly must be ProjectName.ResourceName if building with VS.Net
            SetupResourceAssembly("eliasen.eu.BizTalk.Functoids.eliasen.eu.BizTalk.Functoids.Resources", Assembly.GetExecutingAssembly());

            //Setup the Name, ToolTip, Help Description, and the Bitmap for this functoid
            SetName("Str_ReadAppConfig_Name");
            SetTooltip("Str_ReadAppConfig_Tooltip");
            SetDescription("Str_ReadAppConfig_Description");
            SetBitmap("Bmp_ReadAppConfig_Icon");

            this.SetMinParams(1);
            this.SetMaxParams(1);

            //set the function name that needs to be called when this Functoid is invoked. This means that
            //this Functoid assembly need to be present in GAC for its availability during Test..Map and Runtime.
            SetExternalFunctionName(GetType().Assembly.FullName, "eliasen.eu.BizTalk.Functoids.ReadAppConfig", "GetReadAppConfig");

            //category for this functoid. This functoid goes under the String Functoid Tab in the
            //VS.Net toolbox for functoids.
            this.Category = FunctoidCategory.String;
            this.OutputConnectionType = ConnectionType.AllExceptRecord;

            AddInputConnectionType(ConnectionType.AllExceptRecord); //first input
        }

        //this is the function that gets called when the Map is executed which has this functoid.
        public string GetReadAppConfig(string key)
        {
            System.Configuration.AppSettingsReader app = new System.Configuration.AppSettingsReader();

            string value = (string)app.GetValue(key, typeof(System.String));

            return value != null ? value : string.Empty;
        }
    }
}
